import requests
import config


class LagouCourse:

    def __init__(self):
        self.url_query_course = config.BASE_URL + "/ssm_web/course/findAllCourse"
        self.url_add_course = config.BASE_URL + "/ssm_web/course/saveOrUpdateCourse"
        self.url_modify_course = config.BASE_URL + "/ssm_web/course/saveOrUpdateCourse"
        self.url_update_course = config.BASE_URL + "/ssm_web/course/updateCourseStatus"

    # 查询所有的课程
    def query_all_course(self):
        return requests.post(url=self.url_query_course, headers=config.HEADERS, json={}, cookies=config.COOKIES)

    # 添加课程
    def add_course(self, course_data):
        return requests.post(url=self.url_add_course, json=course_data, headers=config.HEADERS,cookies=config.COOKIES)

    # 修改课程
    def modify_course(self, modify_data):
        return requests.post(url=self.url_add_course, json=modify_data, headers=config.HEADERS,cookies=config.COOKIES)

    # 上下架课程
    def update_course_status(self, course_id, course_status):
        return requests.get(url=self.url_update_course, params={"id": course_id, "status": course_status},
                            headers=config.HEADERS,cookies=config.COOKIES)


if __name__ == '__main__':
    from api_lg_user import LagouUser

    lagou_user = LagouUser()
    lagou_course = LagouCourse()

    response = lagou_user.login("15321919666", "123456")
    print("登陆结果为：", response.json())

    config.HEADERS["Authorization"] = response.json().get("content").get("access_token")
    response = lagou_course.query_all_course()
    print(response.text)

    course_data = {"brief": "走入香格里拉", "courseDescriptionMarkDown": "", "courseImgUrl": "", "courseListImg": "",
                   "courseName": "香格里拉", "discounts": "1", "discountsTag": "泰坦", "id": "",
                   "previewFirstField": "香格里拉", "previewSecondField": "香格里拉", "price": "1", "sales": "1",
                   "sortNum": "", "status": "", "teacherName": "泰坦", "position": "讲师", "description": "讲师简介",
                   "teacherDTO": {}, "activityCourse": False, "activityCourseDTO": {}}

    response = lagou_course.add_course(course_data)
    print(response.json())

    course_data = {"brief": "走入香格里拉2", "courseDescriptionMarkDown": "", "courseImgUrl": "", "courseListImg": "",
                   "courseName": "香格里拉2", "discounts": "1", "discountsTag": "泰坦", "id": "40",
                   "previewFirstField": "香格里拉2", "previewSecondField": "香格里拉2", "price": "1", "sales": "1",
                   "sortNum": "", "status": "", "teacherName": "泰坦", "position": "讲师", "description": "讲师简介",
                   "teacherDTO": {}, "activityCourse": False, "activityCourseDTO": {}}
    response = lagou_course.modify_course(course_data)
    print(response.json())

    response = lagou_course.update_course_status(40, 1)
    print(response.json())